var express = require('express');
var router = express.Router();
var path=require('path');
var CONFIG = require('../../../common/config');
var CODE = require('../../../common/code');
var checkUtil = require('./../util/checkUtil');
var async  = require( 'async'); // 同步控制
var tg3Util  = require('./tg3Util');


//------------------------------------------------------------------
// lua search的redis1的设置
var redis = require("redis");
var redisClient1 = redis.createClient({
    host:CONFIG.LUA_SEARCH_REDIS_SERVER_DOMAIN,
    auth_pass:CONFIG.LUA_SEARCH_REDIS_SERVER_PASSWORD,
    port:CONFIG.LUA_SEARCH_REDIS_SERVER_PORT,
    db:CONFIG.LUA_SEARCH_REDIS_SERVER_DB_NUMBER,
    ttl:CONFIG.LUA_SEARCH_REDIS_SERVER_TTL,
});
var Scripto = require('redis-scripto');
var scriptManager1 = new Scripto(redisClient1);
scriptManager1.loadFromDir(__dirname+'/../../lua2');
scriptManager1.run('common', [], [], function(err, result) {
    // 预执行公共函数
    console.log(err);
});

//------------------------------------------------------------------
// lua search的redis2的设置
var redisClient2 = redis.createClient({
    host:CONFIG.LUA_SEARCH_REDIS2_SERVER_DOMAIN,
    auth_pass:CONFIG.LUA_SEARCH_REDIS2_SERVER_PASSWORD,
    port:CONFIG.LUA_SEARCH_REDIS2_SERVER_PORT,
    db:CONFIG.LUA_SEARCH_REDIS2_SERVER_DB_NUMBER,
    ttl:CONFIG.LUA_SEARCH_REDIS2_SERVER_TTL,
});
var scriptManager2 = new Scripto(redisClient2);
scriptManager2.loadFromDir(__dirname+'/../../lua2');
scriptManager2.run('common', [], [], function(err, result) {
    // 预执行公共函数
    console.log(err);
});

//------------------------------------------------------------------
// lua search的redis-config的设置
var redisClientConfig = redis.createClient({
    host:CONFIG.LUA_SEARCH_REDIS_CONFIG_SERVER_DOMAIN,
    auth_pass:CONFIG.LUA_SEARCH_REDIS_CONFIG_SERVER_PASSWORD,
    port:CONFIG.LUA_SEARCH_REDIS_CONFIG_SERVER_PORT,
    db:CONFIG.LUA_SEARCH_REDIS_CONFIG_SERVER_DB_NUMBER,
    ttl:CONFIG.LUA_SEARCH_REDIS_CONFIG_SERVER_TTL,
});
var scriptManagerConfig = new Scripto(redisClientConfig);
scriptManagerConfig.loadFromDir(__dirname+'/../../lua2');
scriptManagerConfig.run('common', [], [], function(err, result) {
    // 预执行公共函数
    console.log(err);

    // 初始化后即执行
    luaSearchRedisMgrUtil.initConfig(function(){});
});





//------------------------------------------------------------------
var luaSearchRedisMgrUtil = (function () {
    function luaSearchRedisMgrUtil() {
    };



    //------------------------------------------------------------------
    /**
     * 初始化（第一次启动时，需要初始化 redis_config,即设置search_db和search_cache_db）
     * @param 无
     * @param callback
     */
    luaSearchRedisMgrUtil.initConfig = function(callback) {
        var keys    = [];
        var values  = [];
        scriptManagerConfig.run('lua2_init_config', keys, values, function(err, result) {
            callback(err, result);
        });
    }

    //------------------------------------------------------------------
    /**
     * 切换search_db和search_cache_db
     * @param 无
     * @param callback
     */
    luaSearchRedisMgrUtil.switchDB = function(callback) {
        var keys    = [];
        var values  = [];
        scriptManagerConfig.run('lua2_switch_db', keys, values, function(err, result) {
            callback(err, result);
        });
    }

    //------------------------------------------------------------------
    /**
     * 获取search_db
     * @param 无
     * @param callback
     */
    luaSearchRedisMgrUtil.getSearchDB = function(callback) {
        var keys    = [];
        var values  = [];
        scriptManagerConfig.run('lua2_get_search_db', keys, values, function(err, result) {
            callback(err, result);
        });
    }

    //------------------------------------------------------------------
    /**
     * 获取search_db的管理器
     * @param 无
     * @param callback，参数如下
     *          error:
     *          mgr:redis管理器
     */
    luaSearchRedisMgrUtil.getSearchDBMgr = function(callback) {
        var keys    = [];
        var values  = [];
        scriptManagerConfig.run('lua2_get_search_db', keys, values, function(err, result) {
            if(result){
                // 根据配置返回不同的RedisDB管理器
                if(result==1){
                    return callback(null, scriptManager1);
                }else{
                    return callback(null, scriptManager2);
                }
            }else{
                return callback(err, null);
            }

        });
    }
    //------------------------------------------------------------------
    /**
     * 获取search_cache_db的管理器
     * @param 无
     * @param callback，参数如下
     *          error:
     *          mgr:redis管理器
     */
    luaSearchRedisMgrUtil.getCacheDBMgr = function(callback) {
        var keys    = [];
        var values  = [];
        scriptManagerConfig.run('lua2_get_cache_db', keys, values, function(err, result) {
            if(result){
                // 根据配置返回不同的RedisDB管理器
                if(result==1){
                    return callback(null, scriptManager1);
                }else{
                    return callback(null, scriptManager2);
                }
            }else{
                return callback(err, null);
            }
        });
    }
    //------------------------------------------------------------------
    /**
     * 获取config_cache_db的管理器
     * @param 无
     * @param callback，参数如下
     *          error:
     *          mgr:redis管理器
     */
    luaSearchRedisMgrUtil.getConfigDBMgr = function(callback) {
        return callback(null, scriptManagerConfig);
    }




    //------------------------------------------------------------------
    return luaSearchRedisMgrUtil;
})();




//------------------------------------------------------------------
module.exports = luaSearchRedisMgrUtil;

